<?php

declare (strict_types=1);

namespace app\controller;

use app\BaseController;
use think\facade\View;

/**
 * 后台登录
 */
class Login extends BaseController
{

    /**
     * 首页
     */
    public function index()
    {
        return View::fetch('index');
    }

    /**
     * 登录
     */
    public function login()
    {
        // 校验
        $data = $this->request->post();
        $validate = $this->validate($data, 'login');
        if (true !== $validate) {
            return json(result()->fail($validate));
        }
        // 登录
        if (auth()->login($data)) {
            return json(result()->success(['url' => '/backend/index'], '登录成功，跳转中'));
        }

        return json(result()->fail(auth()->error()));
    }

    /**
     * 退出
     */
    public function logout()
    {
        auth()->logout();

        return redirect('/');
    }

}
